package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.Vo.CategoryVo;
import com.boot.pojo.Category;
import com.boot.properties.AccountProperties;
import com.boot.result.Result;
import com.boot.result.ResultCodeNum;
import com.boot.service.CategoryService;
import com.boot.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author liyan
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2025-04-17 15:50:40
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result queryCategoryList(CategoryVo categoryVo) {
        IPage<Category> page = new Page<>(categoryVo.getPage(), categoryVo.getPageSize());
        categoryMapper.queryCategoryList(page, categoryVo);
        List<Category> lists = page.getRecords();
        return Result.success(Map.of("total", page.getTotal(),"records", lists));
    }

    @Override
    public Result addCategory(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort)
                .last("limit 1");
        Category categoryMax = categoryMapper.selectOne(wrapper);
        Integer sort = categoryMax.getSort();
        category.setSort(sort+1);
        category.setStatus(AccountProperties.ENABLE);
        int insertRow = categoryMapper.insert(category);
        if (insertRow > 0) {
            return Result.success();
        }
        return Result.build(null, ResultCodeNum.INSERT_ERROR);
    }

    @Override
    public Result updateCategory(Category category) {
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId, category.getId())
                .set(Category::getName, category.getName())
                .set(Category::getSort, category.getSort());
        int updateRow = categoryMapper.update(category, wrapper);
        if (updateRow > 0) {
            return Result.success();
        }
        return Result.build(null, ResultCodeNum.UPDATE_ERROR);
    }

    @Override
    public Result updateCategoryStatus(Integer status, Integer id) {
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId, id)
                .set(Category::getStatus, status);
        int updateRow = categoryMapper.update(null, wrapper);
        if (updateRow > 0) {
            return Result.success();
        }
        return Result.build(null, ResultCodeNum.UPDATE_ERROR);
    }

    @Override
    public Result deleteCategory(Integer id) {
        int deleteRow = categoryMapper.deleteById(id);
        if (deleteRow > 0) {
            return Result.success();
        }
        return Result.build(null, ResultCodeNum.DELETE_ERROR);
    }

    @Override
    public Result getCategoryByType(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);
        List<Category> list = categoryMapper.selectList(wrapper);
        return Result.success(list);
    }
}




